//
//  SNPickerView.m
//  pickerview_test
//
//  Created by stone on 17/1/15.
//  Copyright © 2017年 stone. All rights reserved.
//

#import "SNPickerView.h"
@interface SNPickerView()<UIPickerViewDataSource, UIPickerViewDelegate>
/** numberOfComponents */
@property (nonatomic, assign) NSInteger sn_numberOfComponents;
/** numberOfRows */
@property (nonatomic, assign) NSInteger sn_numberOfRows;
/** array */
@property (nonatomic,strong) NSArray<NSString *> * models;
/** block */
@property(nonatomic, copy) void (^didSelectRowBlock)(UIPickerView * pickerView,NSInteger row,NSInteger component);

@end
@implementation SNPickerView

- (instancetype)initWithFrame:(CGRect)frame numberOfComponents:(NSInteger)numberOfComponents numberOfRows:(NSInteger)numberOfRows models:(NSArray<NSString *>*)models didSelectRowBlock:(void(^)(UIPickerView * pickerView,NSInteger row,NSInteger component))didSelectRowBlock
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.delegate = self;
        self.dataSource = self;
        self.backgroundColor = [UIColor whiteColor];
        self.sn_numberOfComponents = numberOfComponents;
        self.sn_numberOfRows = numberOfRows;
        
        self.didSelectRowBlock = didSelectRowBlock;
        self.models = models;
    }
    return self;
}
#pragma mark - <UIPickerViewDataSource>
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return self.sn_numberOfComponents;
}
// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return self.sn_numberOfRows;
}
#pragma mark - <UIPickerViewDelegate>
/** 每个组件宽 */
//- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
//    return [UIScreen mainScreen].bounds.size.width / 1;
//}
/** 行高 */
//- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
//    return 44;
//}
/** 告诉pickerview 字符串 */
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    return self.models[row];
}
/** 返回富文本, 字符串更漂亮 */
//- (nullable NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component{
//    return nil;
//}
/** 也能返回自定的view */
//- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view{
//    return nil;
//}
/** 停止转动表示选中, 会调用此方法 */
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    !self.didSelectRowBlock ?: self.didSelectRowBlock(pickerView, row,component);
}
@end
